// 工具函数

// 格式化时间
export const formatTime = (timeString?: string) => {
  if (!timeString) return ''
  
  const date = new Date(timeString)
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
}

// 格式化金额
export const formatAmount = (amount: number) => {
  return `¥${amount.toFixed(2)}`
}

// 验证手机号
export const validateMobile = (mobile: string) => {
  const reg = /^1[3-9]\d{9}$/
  return reg.test(mobile)
}

// 验证验证码
export const validateCode = (code: string) => {
  const reg = /^\d{6}$/
  return reg.test(code)
}

// 验证密码
export const validatePassword = (password: string) => {
  return password.length >= 6
}

// 生成随机ID
export const generateId = () => {
  return 'ID' + Date.now() + Math.random().toString(36).substr(2, 9)
}

// 防抖函数
export const debounce = (func: Function, wait: number) => {
  let timeout: any
  return function (this: any, ...args: any[]) {
    clearTimeout(timeout)
    timeout = setTimeout(() => func.apply(this, args), wait)
  }
}

// 节流函数
export const throttle = (func: Function, limit: number) => {
  let inThrottle: boolean
  return function (this: any, ...args: any[]) {
    if (!inThrottle) {
      func.apply(this, args)
      inThrottle = true
      setTimeout(() => inThrottle = false, limit)
    }
  }
}